# -*- coding: utf-8 -*-
"""
Created on Fri Oct 16 10:19:04 2020

@author: Albert Darren
"""
import numpy as np


def experiment1():
    # 1.给定集合A={a,b,c,d,e}和集合B={c,e,f,g}，求A和B的并集、交集、差集和对称差
    A = {'a', 'b', 'c', 'd', 'e'}
    B = {'c', 'e', 'f', 'g'}
    print("""
集合A:{}
集合B:{}
""".format(A, B) + "-" * 30)
    print("""
并集：{}
交集：{}
差集：{}
对称差：{}
""".format(A.union(B), A.intersection(B), A.difference(B), A.symmetric_difference(B
                                                                                  )))


def experiment2():
    """
    2. 给定集合A={1,2,3,4}和集合B={a,b,c}，
    求这两个集合的笛卡尔积中的所有元素
    """
    A = {1, 2, 3, 4}
    B = {'b', 'c', 'a'}
    print("""
集合A:{}
集合B:{}
""".format(A, B) + "-" * 30)
    print("笛卡尔积{}x{}=\n{}".format(str(A), str(B), cartesian_product(A, B)))


def cartesian_product(set_a: set, set_b: set):
    new_set = set()
    for item1 in set_a:
        for item2 in set_b:
            item_set = set()
            item_set.add(item1)
            item_set.add(item2)
            new_set.add("{}".format(item_set))

    return new_set


def experiment3():
    """
    已知集合A={1,2,3,4}，求A上的整除关系及其矩阵表示
    """
    A = {1, 2, 3, 4}
    print("""
集合A:{}上的整除关系   
""".format(A) + "-" * 29)
    row_list = []
    for first_element in A:
        for second_element in A:
            expr1 = second_element / first_element
            expr2 = second_element // first_element
            row_list.append(int(expr1 == expr2))

    matrix = np.array(row_list).reshape((4, 4))
    print(matrix)


if __name__ == "__main__":
    experiment1()
    experiment2()
    experiment3()
